
patronage_data <-
  main_df %>%
  filter(group.treatment == "patronage") %>%
  group_by(cluster) %>%
  mutate(bos_ethnicity = player.ethnicity[bos_indicator == 1],
         bos_gender = player.gender[bos_indicator == 1],
         bos_religion = player.gender[bos_indicator == 1],
         bos_education = player.education[bos_indicator == 1],
         bos_location = player.province[bos_indicator == 1]) %>%
  mutate(match_ethnicity = player.ethnicity == bos_ethnicity,
         match_gender = player.gender == bos_gender,
         match_religion = player.religion == bos_religion,
         match_education = player.education == bos_education,
         match_location = player.province == bos_location) %>%
  filter(bos_indicator != 1)

patronage_test <- lm(pub_official_indicator ~ match_ethnicity + match_gender + match_religion + match_education + match_location, data = patronage_data)


observations <- c(nobs(patronage_test))

patronage_test <- coeftest(patronage_test, vcov=vcovHC(patronage_test,type="HC0"), cluster = ~cluster)


table <- list(patronage_test)

note_text <- paste("Beta coefficients from OLS regression. Standard errors were calculated using the Huber-White (HC0) correction. 
                   The outcome is an indicator variable capturing demographic match across the patron and the selected public official.")

table = stargazer(table, type = 'latex', 
                  title = "The Effect of Demographic Traits on Selection under Patronage",
                  label = 'tab:patron_select',
                  model.names = F,
                  model.numbers = T,
                  digits = 3,
                  
                  dep.var.labels = "Dependent Variable: Selected", 
                  add.lines = list(
                    c("Observations", observations)),
                  covariate.labels = c("Match: Ethnicity", "Match: Gender", "Match: Religion", "Match: Education Level", "Match: Province"),
                  
                  #star.cutoffs = c(0.05, 0.01),
                  #float.env = 'sidewaystable',
                  keep.stat = c("n"),
                  notes = NULL,
                  notes.align = 'l')

write_latex(table[-c(10, 11, 16, 19, 22, 25, 28, 31, 36)], note_text, './outputs/tables/table_a14.tex', .8)


